#ifndef __SEMAPHORE_H
#define __SEMAPHORE_H

#include "../rtx/rtx_os.h"
#include "../utils/nocopyable.h"

namespace rtos {

class Semaphore : private utils::NoCopyable {
public:
    void init(uint32_t max_count = 1, uint32_t init_count = 0,
              const char *name = "other") {
        osSemaphoreAttr_t attr;
        attr.name = name;
        attr.attr_bits = 0;
        attr.cb_mem = m_cb_mem;
        attr.cb_size = osRtxSemaphoreCbSize;
        m_id = osSemaphoreNew(max_count, init_count, &attr);
    }

    inline osStatus_t acquire(uint32_t timeout = osWaitForever) {
        return osSemaphoreAcquire(m_id, timeout);
    }

    inline osStatus_t release() { return osSemaphoreRelease(m_id); }

private:
    osSemaphoreId_t m_id;
    uint64_t m_cb_mem[(osRtxSemaphoreCbSize + 7) / 8];
};

} // namespace rtos

#endif
